package com.lbm.business.web.utils;

import javax.servlet.ServletException;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import javax.servlet.jsp.PageContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;

/**
 * <p> Title:下载文件处理</p>
 * <p> Description:</p>
 * <p> Copyright: Copyright (c) 2011 </p>
 * <p> Company:ssa.co.,ltd </p>
 *  @author zhuq
 * @version 1.0
 */
public class DownloadFile {

	protected HttpServletRequest m_request;

	protected HttpServletResponse m_response;

	protected ServletContext m_application;

	public DownloadFile() {
	}

	public final void initialize(PageContext pagecontext)
			throws ServletException {
		m_application = pagecontext.getServletContext();
		m_request = (HttpServletRequest) pagecontext.getRequest();
		m_response = (HttpServletResponse) pagecontext.getResponse();
	}

	public void downloadFile(String strFilePath, String strName)
			throws ServletException, IOException {
		downloadFile(strFilePath, strName, null, null);
	}

	public void downloadFile(String strFilePath, String strName,
			String strContentType) throws ServletException, IOException {
		downloadFile(strFilePath, strName, strContentType, null);
	}

	public void downloadFile(String strFilePath, String strFileName,
			String strContentType, String strDisp) throws ServletException,
			IOException {
		downloadFile(strFilePath, strFileName, strContentType, strDisp, 8192);
	}

	/**
	 * 下载文件到客户端
	 * @param strFileName 文件名
	 * @param strContentType 下载类型
	 * @param s2 附件类型
	 * @param btLen byte的最大长度
	 * @throws ServletException
	 * @throws IOException
	 */
	public void downloadFile(String strFilePath, String strFileName,
			String strContentType, String strDisp, int btLen)
			throws ServletException, IOException {

		if (strFilePath == null)
			throw new IllegalArgumentException("File '" + strFilePath
					+ "' not found (1040).");
		if (strFilePath.equals(""))
			throw new IllegalArgumentException("File '" + strFilePath
					+ "' not found (1040).");
		File file = null;
		try {
			file = new File(strFilePath, strFileName);
			// file = new File(strFilePath, new String(strFileName
			// .getBytes("ISO8859-1"), "GBK"));
			if (file == null || file.equals("")) {
				throw new Exception("系统找不到指定文件!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		FileInputStream fileinputstream = new FileInputStream(file);
		long lngSize = file.length();
		byte abyte0[] = new byte[btLen];
		// 设置下载类型
		if (m_application.getMimeType(file.getName()) != null) {
			m_response
					.setContentType(m_application.getMimeType(file.getName()));
			m_response.setHeader("Content-Type",
					m_application.getMimeType(file.getName()));
		} else {
			m_response.setContentType("application/x-msdownload");
		}
		m_response.setHeader("Content-Disposition", "attachment;filename="
				+ new String( strFileName.getBytes("GBK"), "ISO8859-1" ));

		m_response.setContentLength((int) lngSize);

		int j;
		m_response.setBufferSize((int) file.length());
		OutputStream out = m_response.getOutputStream();

		while ((j = fileinputstream.read(abyte0, 0, btLen)) != -1) {
			out.write(abyte0, 0, j);
		}
		fileinputstream.close();
		out.close();

	}

}
